﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace WoChu.CF.BaseFrame.WebClient
{
    /// <summary>
    /// 
    /// </summary>
    public static class ClientChannelFactory
    {
        private static Dictionary<Type, string> _WcfInvokeContainer = new Dictionary<Type, string>();
        //public static string WEB_URL_ROOT_PATH = "http://192.168.125.73/testRfWcfService1/{0}.svc";
        //public static string WEB_URL_ROOT_PATH = "http://192.168.125.87/MBShopRfWcfService/{0}.svc";
        //public static string WEB_URL_ROOT_PATH = "http://192.168.204.162:18090/{0}.svc";
        public static string WEB_URL_ROOT_PATH = string.Empty;

        //public static string WEB_URL_ROOT_PATH = "http://192.168.103.139:18090/{0}.svc";

        /// <summary>
        /// WCF 服务调用注册.
        /// </summary>
        /// <param name="iServiceType"></param>
        /// <param name="relativeUrlPath"></param>
        public static void RegisterTypeIfMissing(Type iServiceType, string relativeUrlPath)
        {
            if (!_WcfInvokeContainer.ContainsKey(iServiceType))
            {
                _WcfInvokeContainer.Add(iServiceType, relativeUrlPath);
            }
        }

        public static TResult InvokeWebMethod<T, TResult>(Func<T,TResult> execute)
        {
            //try
            //{
            //    string methodUrl = "";
            //    List<PropertyInfo> proList = new List<PropertyInfo>();
            //    proList.AddRange(typeof(T).GetProperties(BindingFlags.Public));

            //    foreach (PropertyInfo prop in proList)
            //    {
            //        string method = prop.Name;
            //        if (method.Equals(execute.Method.Name))
            //        {
            //            MappingAttribute mapUrl = (MappingAttribute)prop.GetCustomAttributes(typeof(MappingAttribute), false)[0];
            //            methodUrl = mapUrl.MethodUrl;

            //            MethodInfo mt = prop.GetGetMethod();

            //            ParameterInfo[] arrParam = mt.GetParameters();
            //            for (int i = 0; i < arrParam.Length; i++)
            //            {
            //                methodUrl = methodUrl.Replace("{" + i + "}", arrParam[i].Name);
            //            }

            //            break;
            //        }
            //    }

            //    return default(TResult);
            //}
            //catch (Exception ex)
            //{
            //    CF.BaseFrame.Util.TraceEx.Write(string.Format("调用服务{0} 方法出错,请查阅服务端日记 " + ex.Message, typeof(T).FullName), Util.SysMessageLevel.SysErrInfo);
            //    throw CF.BaseFrame.Util.RfAppException.CreateAppException(ex.Message, Util.SysMessageLevel.SysErrInfo);
            //}
            return default(TResult);
        }

        //public static TResult GetInvokeMethod<TResult>(string serviceName, string methodUrl, object[] objParams)
        //{
        //    using (ClientChannel scope = new ClientChannel(serviceName))
        //    {
        //        try
        //        {
        //            for (int i = 0; i < objParams.Length; i++)
        //            {
        //                methodUrl = methodUrl.Replace("{" + i + "}", objParams[i].ToString());
        //            }

        //            return (TResult)scope.Get<TResult>(methodUrl);
        //        }
        //        catch (Exception ex)
        //        {
        //            CF.BaseFrame.Util.TraceEx.Write(string.Format("调用服务{0} 方法出错,请查阅服务端日记 " + ex.Message, serviceName), Util.SysMessageLevel.SysErrInfo);
        //            throw CF.BaseFrame.Util.RfAppException.CreateAppException(ex.Message, Util.SysMessageLevel.SysErrInfo);
        //        }
        //    }
        //}

        //public static TResult PostInvokeMethod<TInput,TResult>(string serviceName, string methodUrl, object[] objParams, TInput data)
        //{
        //    using (ClientChannel scope = new ClientChannel(serviceName))
        //    {
        //        try
        //        {
        //            for (int i = 0; i < objParams.Length; i++)
        //            {
        //                methodUrl = methodUrl.Replace("{" + i + "}", objParams[i].ToString());
        //            }

        //            return (TResult)scope.Post<TInput, TResult>(data,methodUrl);
        //        }
        //        catch (Exception ex)
        //        {
        //            CF.BaseFrame.Util.TraceEx.Write(string.Format("调用服务{0} 方法出错,请查阅服务端日记 " + ex.Message, serviceName), Util.SysMessageLevel.SysErrInfo);
        //            throw CF.BaseFrame.Util.RfAppException.CreateAppException(ex.Message, Util.SysMessageLevel.SysErrInfo);
        //        }
        //    }
        //}
    }
}
